package com.sample.android.main;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sample.android.dao.UtilisateurDataSource;
import com.sample.android.entity.Utilisateur;
import com.sample.android.sqlite.AfficherBase;
import com.sample.android.sqlite.TestMySQLiActivity;
import com.sample.android.wifi.R;
import com.sample.android.wifi.WiFiDirectActivity;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sample.android.dao.*;

public class LoginActivity extends Activity implements OnClickListener,
		syncUserFragment.TaskCallbacks
{
	private UtilisateurDataSource	DAOutilisateur;
	private static final String		TAG_SYNC_FRAGMENT	= "syncUser_fragment";
	private syncUserFragment		mSynFragment;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		/* GESTION DU LOGIN DE L'UTILISATEUR */
		DAOutilisateur = new UtilisateurDataSource(this);
		DAOutilisateur.open();

		List<Utilisateur> values = DAOutilisateur.getAllUtilisateurs();
		if (values.isEmpty() == true)
		{
			setContentView(R.layout.login);
			findViewById(R.id.btnSingIn).setOnClickListener(this);
		}
		else
		{
			intent = new Intent(LoginActivity.this, MainActivity.class);
			startActivity(intent);
		}
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}

	Intent	intent;

	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.btnSingIn:
				EditText Edit = (EditText) findViewById(R.id.etUserName);
				String etUserName = Edit.getText().toString();
				Edit = (EditText) findViewById(R.id.etPass);
				String etPass = Edit.getText().toString();
				if (etUserName != null && etPass != null)
				{
					FragmentManager fm = getFragmentManager();
					mSynFragment = (syncUserFragment) fm
							.findFragmentByTag(TAG_SYNC_FRAGMENT);

					mSynFragment = new syncUserFragment();
					Bundle bundl = new Bundle();
					bundl.putString("email", etUserName);
					bundl.putString("mdp", etPass);
					mSynFragment.setArguments(bundl);
					fm.beginTransaction().add(mSynFragment, TAG_SYNC_FRAGMENT)
							.commit();

				}
				else
				{
					Toast.makeText(this, "Please complete the form.",
							Toast.LENGTH_SHORT).show();
				}
				break;
		}
	}

	@Override
	public void onPreExecute()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onProgressUpdate(int percent)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onCancelled()
	{
		// TODO Auto-generated method stub
		Toast.makeText(this, "Check incompleted.", Toast.LENGTH_SHORT).show();

	}

	@Override
	public void onPostExecute()
	{
		// TODO Auto-generated method stub
	}

	@Override
	public void processFinish(int output, Utilisateur utilisateur)
	{
		// TODO Auto-generated method stub
		if (output != 0)
		{
			DAOutilisateur.createUtilisateur(utilisateur);
			intent = new Intent(LoginActivity.this, MainActivity.class);
			startActivity(intent);
		}
		else
		{
			Toast.makeText(this, "Invalid mail or password.",
					Toast.LENGTH_SHORT).show();
			mSynFragment.onDestroy();
		}
	}
}
